package com.gears_software.sxdc.dccx.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.adapter.MessageCenterAdapter;
import com.gears_software.sxdc.dccx.data.db.DBUtils;
import com.gears_software.sxdc.dccx.data.db.DBUtils_;
import com.gears_software.sxdc.dccx.data.model.AddZPOrderModel;
import com.gears_software.sxdc.dccx.data.model.NoticeModel;
import com.gears_software.sxdc.dccx.data.sharedPreference.DCCXSp_;
import com.github.mzule.activityrouter.annotation.Router;
import com.orhanobut.logger.Logger;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.sharedpreferences.Pref;

import java.util.ArrayList;
import java.util.List;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by SunMuran on 2017/7/27.
 * 消息中心
 */
@EActivity(R.layout.message_center)
@Router(value = "messagecenter", isEActivity = true)
public class MessageCenterActivity extends Activity {
    @App
    DCCXApplication app;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    ListView list_message;
    @ViewById
    RelativeLayout re_wu;
    @ViewById
    TextView txt_nodata_icon;
    @Bean
    DBUtils dbUtils;
    @Pref
    DCCXSp_ sp;
    MessageCenterAdapter adapter;
    List<NoticeModel> list = new ArrayList<NoticeModel>();
    Context context;
    int MENU_DEL = 1;

    @AfterViews
    void created() {
        context = this;
        actionbar_title.setText("消息中心");
        initIconfont();
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorActionbar));

//        NoticeModel noticeModel = new NoticeModel(2,"aaa","1","aaaaaaaaaaabbbbbbbb","2017-09-21 13:12");
//        dbUtils.insertNotice(noticeModel);

        if (!sp.userId().get().equals("")) {
            list = dbUtils.getNoticeList(sp.userId().get());
            if (list != null && list.size() > 0) {
                right_icon.setVisibility(View.VISIBLE);
                adapter = new MessageCenterAdapter(this, list);
                list_message.setAdapter(adapter);
                re_wu.setVisibility(View.GONE);
                registerForContextMenu(list_message);
            } else {
                right_icon.setVisibility(View.GONE);
                re_wu.setVisibility(View.VISIBLE);
                list_message.setVisibility(View.GONE);
            }
        }


    }

    void initIconfont() {
        back_icon.setTypeface(app.iconfont);
        back_icon.setText(R.string.back_icon);
        right_icon.setVisibility(View.GONE);
        right_icon.setText("全部清除");
        right_icon.setTextSize(16);
        txt_nodata_icon.setTypeface(app.iconfont);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, MENU_DEL, 0, "删除记录");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        //获取上下文菜单适配器
        AdapterView.AdapterContextMenuInfo cmi = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        //获取被选择的菜单位置
        final int position = cmi.position;

        if (item.getItemId() == MENU_DEL) {
            new MaterialDialog.Builder(this)
                    .title("提示")
                    .titleGravity(GravityEnum.CENTER)
                    .titleColorRes(R.color.colorFontGray)
                    .content("确定删除选中信息吗？")
                    .contentColorRes(R.color.black)
                    .positiveText("确定")
                    .positiveColor(ContextCompat.getColor(this, R.color.colorFontBlue))
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            boolean flag = dbUtils.deleteNoticeById(list.get(position).getId());
                            if (flag) {
                                list.remove(position);
                                adapter.notifyDataSetChanged();
                                TastyToast.makeText(MessageCenterActivity.this, "删除成功", app.SHORT, app.SUCCESS);
                            } else {
                                TastyToast.makeText(MessageCenterActivity.this, "删除失败", app.SHORT, app.ERROR);
                            }
                        }
                    })
                    .negativeText("取消")
                    .negativeColor(ContextCompat.getColor(this, R.color.colorFontGray))
                    .onNegative(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            dialog.dismiss();
                        }
                    })
                    .buttonsGravity(GravityEnum.CENTER)
                    .show();

        }
        return super.onContextItemSelected(item);
    }

    @Click
    void right_icon() {
        new MaterialDialog.Builder(this)
                .title("提示")
                .titleGravity(GravityEnum.CENTER)
                .titleColorRes(R.color.colorFontGray)
                .content("您确定清除全部消息信息吗?")
                .contentColorRes(R.color.black)
                .positiveText("确定")
                .positiveColor(ContextCompat.getColor(this, R.color.colorFontBlue))
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        if (list != null && list.size() > 0) {
                            boolean isDelete = dbUtils.removeNoticeList(sp.userId().get());
                            if (isDelete) {
                                TastyToast.makeText(context, "消息清除成功", app.SHORT, app.SUCCESS);
                                list.clear();
                                adapter.notifyDataSetChanged();
                            } else {
                                TastyToast.makeText(context, "消息清除失败,请稍后再试", app.SHORT, app.ERROR);
                            }
                        }

                    }
                })
                .negativeText("取消")
                .negativeColor(ContextCompat.getColor(this, R.color.colorFontGray))
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        dialog.dismiss();
                    }
                })
                .buttonsGravity(GravityEnum.CENTER)
                .show();
    }

    @Click
    void back_icon() {
        finish();
    }

}
